/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ThemNguoi.java
 *
 * Created on Dec 18, 2012, 5:21:40 AM
 */
package CLIENT;

import DTO.*;
import BLL.KhachHangBLL;
import BLL.RoomBLL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.sql.Date;
/**
 *
 * @author Huong Giang
 */
public class ThemNguoi extends javax.swing.JFrame {
    /** Creates new form ThemNguoi */
    public ThemNguoi() {
        initComponents();
        this.loadData();     
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtCMT = new javax.swing.JTextField();
        txtTen = new javax.swing.JTextField();
        txtNgayTra = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        cbMaphong = new javax.swing.JComboBox();
        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Thêm người thuê");

        jLabel1.setText("CMT:");

        jLabel2.setText("Tên:");

        jLabel3.setText("Mã phòng:");

        jLabel4.setText("Ngày trả:");

        jLabel6.setText("Kiểu thanh toán:");

        txtCMT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCMTActionPerformed(evt);
            }
        });

        txtNgayTra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNgayTraActionPerformed(evt);
            }
        });

        jButton1.setText("Thuê phòng");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setText("Về Menu");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("jButton4");

        jButton5.setText("Thoát");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "null"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        cbMaphong.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Lựa chọn mã phòng" }));
        cbMaphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbMaphongActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Tháng", "Quý", "Năm" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(jButton1)
                        .addGap(109, 109, 109)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 120, Short.MAX_VALUE)
                        .addComponent(jButton5)
                        .addGap(103, 103, 103))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 590, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(20, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTen, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCMT, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbMaphong, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1)))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtNgayTra, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(63, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtCMT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtTen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(cbMaphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNgayTra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5)
                    .addComponent(jButton1)
                    .addComponent(jButton3))
                .addContainerGap(42, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txtCMTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCMTActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtCMTActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            KhachHang emp = new KhachHang();
            if ("".equals(txtCMT.getText()))
            {
                JOptionPane.showMessageDialog(this, "CMT không thể đển trống");
            }
            else{
            if ("".equals(txtTen.getText()))
            {
                JOptionPane.showMessageDialog(this, "Tên khách hàng không thể đển trống");
            }
            else{
            if ("".equals(jComboBox1.getSelectedItem().toString()))
            {
                JOptionPane.showMessageDialog(this, "Kiểu thanh toán không thể đển trống");
            }else{
            if ("".equals(txtNgayTra.getText()))
            {
                JOptionPane.showMessageDialog(this, "Ngày trả không thể đển trống");
            }else{
        emp.setCmt(Integer.parseInt(this.txtCMT.getText()));
        emp.setTen(this.txtTen.getText());
        int ma= Integer.parseInt(this.cbMaphong.getSelectedItem().toString());
        emp.setMaPhong(ma);
        emp.setKieuTT(this.jComboBox1.getSelectedItem().toString());
        Date ngaythue = new Date(System.currentTimeMillis());
        emp.setNgayThue(ngaythue);
         SimpleDateFormat dateformat=new SimpleDateFormat("dd/MM/yyyy"); 
        java.util.Date date=dateformat.parse(this.txtNgayTra.getText());
        java.sql.Date d= new java.sql.Date(date.getTime());
        emp.setNgayTra(d);
        boolean result = KhachHangBLL.insertKH(emp);
        if (result) {
            JOptionPane.showMessageDialog(this, "Thêm khách hàng thành công");
            this.cbMaphong.removeAllItems();
            this.loadData();
            RoomBLL.updateThue(ma);
            this.txtCMT.setText("");
            this.txtCMT.requestFocus();
            this.txtNgayTra.setText("");
            this.txtTen.setText("");
        } else {
            JOptionPane.showMessageDialog(this, "Lỗi, nhập lại");
        }
        } 
            }}}}
        catch (ParseException ex) {
            Logger.getLogger(ThemNguoi.class.getName()).log(Level.SEVERE, null, ex);
        }
           
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        Main main=new Main();
        main.show();
        this.dispose();
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        this.dispose();
}//GEN-LAST:event_jButton5ActionPerformed

private void cbMaphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbMaphongActionPerformed
        
}//GEN-LAST:event_cbMaphongActionPerformed

private void txtNgayTraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNgayTraActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtNgayTraActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ThemNguoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ThemNguoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ThemNguoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ThemNguoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ThemNguoi().setVisible(true);
            }
        });
      }
     public final void loadData() {
         ArrayList<KhachHang> list=KhachHangBLL.selectKHall();
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("CMT");
        dtm.addColumn("Tên");
        dtm.addColumn("Mã phòng");
        dtm.addColumn("Kiểu thanh toán");
        dtm.addColumn("Ngày thuê");
        dtm.addColumn("Ngày trả");
        for (int i = 0; i < list.size(); i++) {
            KhachHang emp = list.get(i);
            Vector vec = new Vector();
            vec.add(emp.getCmt());
            vec.add(emp.getTen());
            vec.add(emp.getMaPhong());
            vec.add(emp.getKieuTT());
            vec.add(emp.getNgayThue());
            vec.add(emp.getNgayTra());
            dtm.addRow(vec);
        }
        this.jTable1.setModel(dtm);
         try
         {
             int[] mp= RoomBLL.SelectMaPhong();
            for(int i=0;i<mp.length;i++)
             {
             if(mp[i]!=0){
             this.cbMaphong.addItem(mp[i]);}}
              } catch (SQLException ex) {
            Logger.getLogger(ThemNguoi.class.getName()).log(Level.SEVERE, null, ex);
        }} 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbMaphong;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField txtCMT;
    private javax.swing.JTextField txtNgayTra;
    private javax.swing.JTextField txtTen;
    // End of variables declaration//GEN-END:variables
}
